package demo.demo08;

import java.net.InetAddress;
import java.net.UnknownHostException;

//网络编程
public class testINetAddress {
    public static void main(String[] args) {
        try {
            //查询本机ip地址
            InetAddress localhost = InetAddress.getByName("localhost");
            System.out.println(localhost);
            InetAddress localhost1 = InetAddress.getByName("127.0.0.1");
            System.out.println(localhost1);
            InetAddress localhost2 = InetAddress.getLocalHost();
            System.out.println(localhost2);

            //查询网络ip地址
            InetAddress byName = InetAddress.getByName("www.baidu.com");
            System.out.println(byName);

            //常用方法
            System.out.println(byName.getAddress());
            System.out.println(byName.getCanonicalHostName());//规范的名字
            System.out.println(byName.getHostAddress());//ip
            System.out.println(byName.getHostName());//域名
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
